﻿using JGSoft.U9CRTAPIEX.DTOs.RequestDTOs;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JGSoft.U9CRTAPIEX.Public
{
    public static class OpenAPIAuth
    {
        public static YunmaAuthDto YunmaAuth()
        {
            YunmaAuthDto dto = new YunmaAuthDto();
            JsonConfig config = new JsonConfig();
            string url = config.SelectToken("Yunma.URL");
            HttpHelper httpHelper = new HttpHelper();
            httpHelper.URL = url + $"/openApi/user/login?name={config.SelectToken("Yunma.name")}&pwd={config.SelectToken("Yunma.pwd")}";
            var result = JObject.Parse(httpHelper.Request("GET"));
            if (result.Value<bool>("success"))
            {
                dto.token = result["data"].Value<string>("token");
                dto.enterprise_id = result["data"].Value<string>("enterprise_id");
                return dto;
            }
            else
            {
                throw new Exception("云码认证接口失败:" + result.Value<string>("msg"));
            }

        }
    }
}
